(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:GzhmemberCtrl
     * @description
     * # GzhmemberCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('GzhmemberCtrl', GzhmemberCtrl);

    GzhmemberCtrl.$inject = ['$scope', 'dataService', '$location', 'helper'];

    function GzhmemberCtrl($scope, dataService, $location, helper) {

        $scope.listAction = function() {
            $scope.pageSize = 10;
            $scope.getlist(1);
        }


        $scope.synchronization = function() {
            dataService.phpsynchronizationData({ app_id: $scope.wxappid }).then(function(list) {
                if (list.Msg) {
                    console.error(list.Msg);
                }
            });
        }

        $scope.memberinfoAction = function() {
            $scope.openid = $location.search().openid;
            $scope.getOne();

        }

        $scope.getlist = function(pageno) {
            if ($scope.wxappid) {
                $scope.pageNo = pageno ? pageno : "1";
                var query = { page_no: $scope.pageNo, page_size: $scope.pageSize, "app_id": $scope.wxappid };
                if ($scope.nickname) {
                    query.nickname = $scope.nickname;
                }
                if ($scope.sex) {
                    query.sex = $scope.sex;
                }
                if ($scope.wxappid) {
                    query.app_id = $scope.wxappid;
                }
                if ($scope.subscribe || $scope.subscribe == 0) {
                    query.subscribe = $scope.subscribe;
                }

                if ($scope.tag) {
                    query.tag = $scope.tag;
                }

                console.error("$scope.tag",$scope.tag,$scope.subscribe_scene);
                if ($scope.subscribe_scene) {
                    query.subscribe_scene = $scope.subscribe_scene;
                }
                dataService.phpWxgzhmemberGet(query).then(function(list) {
                    if (list.list) {
                        $scope.users = list.list;
                        $scope.total_count = list.total_count;
                        if (list.total_count == 0) {
                            $scope.synchronization();
                        }
                    }
                });
            } else {
                console.log("app_id不能为空！");
            }
        }


        $scope.getOne = function() {
            var query = { openid: $scope.openid };
            console.log("query", query);
            dataService.phpWxgzhmemberGet(query).then(function(list) {
                if (list.list) {
                    $scope.member = list.list[0];
                }
            });
        }
        $scope.wxgzhcallback = function(wxappid) {
            $scope.wxappid = wxappid;
            $scope.getlist(1);
        }

        $scope.redbagAction = function() {
            $scope.app_id = $location.search().app_id;
            $scope.bag = { "app_id": $scope.app_id };
            $scope.bag.open_id = $location.search().openid;
        }

        $scope.redpackAdd = function() {
            console.log($scope.bag);
            dataService.phpredpack($scope.bag).then(function(list) {
                if (list.list) {
                    $scope.member = list.list[0];
                }
            });
        }
    }
})();